package hu.kisprof.quiz.view.dialogs;

import hu.kisprof.quiz.view.Labels;
import hu.kisprof.quiz.view.QuizGUI;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class QuizChallengeDialog extends JDialog implements ActionListener{
	
	private static final long serialVersionUID = 8292424366434060237L;
	private QuizGUI gui;
	private static String[] difficulties={"1","2","3","4","5","6","7","8","9","10"};
	private JTextArea messageTA = new JTextArea();
	private JComboBox difficulty = new JComboBox(difficulties);
	
	private JButton chllButton = new JButton(Labels.challange);
	private JButton cancelButton = new JButton(Labels.cancel);
	
	private String username;
	
	
	public QuizChallengeDialog(String username, QuizGUI gui){
		super(gui.getWindow(),Labels.challengePlayer);
		this.gui = gui;
		this.username = username;
		JPanel dialogPanel = new JPanel();
		dialogPanel.setLayout(new BorderLayout());
		dialogPanel.add(createSettingsPanel(), BorderLayout.CENTER);
		dialogPanel.add(createButtonsPanel(), BorderLayout.SOUTH);
		
		getContentPane().add(dialogPanel);
		setPreferredSize(new Dimension(300,250));
		pack();
		setLocationRelativeTo(gui.getWindow());
		setVisible(true);
	}



	private JPanel createButtonsPanel() {
		JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));
		buttons.add(chllButton);
		chllButton.addActionListener(this);
		buttons.add(cancelButton);
		cancelButton.addActionListener(this);
		
		return buttons;
		
	}



	private Component createSettingsPanel() {
		JPanel settings = new JPanel(new BorderLayout());
		JPanel tmpPanel = new JPanel(new GridLayout(1,2));
		tmpPanel.add(new JLabel(Labels.difficulty+":"));
		tmpPanel.add(difficulty);
		settings.add(tmpPanel, BorderLayout.NORTH);
		tmpPanel=new JPanel(new BorderLayout());
		tmpPanel.add(new JLabel(Labels.message+":"),BorderLayout.NORTH);
		tmpPanel.add(messageTA, BorderLayout.CENTER);
		settings.add(tmpPanel, BorderLayout.CENTER);
		
		return settings;
	}



	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals(Labels.challange)){
			setVisible(false);
			new QuizChallengeSentDialog(username, gui, messageTA.getText(),Integer.parseInt((String)difficulty.getSelectedItem()));
		}
		if(e.getActionCommand().equals(Labels.cancel)){
			setVisible(false);
		}
		
	}
}
